package pannel.order;

import system.BasePage;
import pannel.user.User;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.List;

public class OrderManagementPage extends BasePage {
    private DefaultTableModel model;
    private JTable table;
    private JTextField searchField;
    private int currentPage = 1;
    private int pageSize = 10;
    private JLabel pageInfoLabel;
    private OrderDao orderDao;
    private int totalRecords = 0;

    public OrderManagementPage(User user) {
        super(user);
        orderDao = new OrderDao();
        initPageContent();
    }

    @Override
    protected void initPageContent() {
        setPageTitle("订单管理");
        setupContent();
        refreshTable();
    }

    private void setupContent() {
        contentArea.setLayout(new BorderLayout());
        JPanel topPanel = new JPanel(new BorderLayout());

        // 搜索面板
        JPanel searchPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        searchField = new JTextField(20);
        JButton searchButton = new JButton("搜索");
        searchPanel.add(new JLabel("搜索:"));
        searchPanel.add(searchField);
        searchPanel.add(searchButton);

        topPanel.add(searchPanel, BorderLayout.NORTH);

        // 表格设置
        String[] columnNames = {"订单ID", "用户", "商品", "数量", "总价", "状态", "创建时间"};
        model = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };
        table = new JTable(model);

        // 分页面板
        JPanel paginationPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        JButton prevButton = new JButton("上一页");
        JButton nextButton = new JButton("下一页");
        pageInfoLabel = new JLabel();
        paginationPanel.add(prevButton);
        paginationPanel.add(pageInfoLabel);
        paginationPanel.add(nextButton);

        // 按钮事件
        searchButton.addActionListener(e -> {
            currentPage = 1;
            refreshTable();
        });

        prevButton.addActionListener(e -> {
            if (currentPage > 1) {
                currentPage--;
                refreshTable();
            }
        });

        nextButton.addActionListener(e -> {
            int totalPages = (int) Math.ceil((double) totalRecords / pageSize);
            if (currentPage < totalPages) {
                currentPage++;
                refreshTable();
            }
        });

        JScrollPane scrollPane = new JScrollPane(table);
        contentArea.add(topPanel, BorderLayout.NORTH);
        contentArea.add(scrollPane, BorderLayout.CENTER);
        contentArea.add(paginationPanel, BorderLayout.SOUTH);
    }

    private void refreshTable() {
        SwingUtilities.invokeLater(() -> {
            try {
                String searchText = searchField.getText().trim();
                List<Order> orders = orderDao.pageQueryAll(searchText, currentPage, pageSize);
                totalRecords = orderDao.getTotalCount(searchText);

                model.setRowCount(0);
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                for (Order order : orders) {
                    model.addRow(new Object[]{
                            order.getId(),
                            order.getUserName(),
                            order.getProductName(),
                            order.getQuantity(),
                            String.format("￥%.2f", order.getTotalPrice()),
                            order.getStatus(),
                            sdf.format(order.getCreateTime())
                    });
                }

                updatePageInfo();
            } catch (SQLException e) {
                JOptionPane.showMessageDialog(this, "获取订单数据失败：" + e.getMessage());
            }
        });
    }

    private void updatePageInfo() {
        int totalPages = (int) Math.ceil((double) totalRecords / pageSize);
        pageInfoLabel.setText(String.format("第 %d/%d 页  共%d条记录", currentPage, totalPages, totalRecords));
    }
} 